package com.anolesoft.yr.mbus.fin.mapper;

import com.anolesoft.yr.mbus.fin.entity.FinBillingApplyDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/12 15:12
 */
@Mapper
public interface FinBillingApplyMapper {
    @Insert("insert into fin_billing_apply(" +
            "objuid," +
            "fac_code," +
            "contract_code," +
            "billing_code," +
            "amount," +
            "invoice_state," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ") values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{contractCode,jdbcType=VARCHAR}," +
            "#{billingCode,jdbcType=VARCHAR}," +
            "#{amount,jdbcType=DECIMAL}," +
            "#{invoiceState,jdbcType=VARCHAR}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(FinBillingApplyDO finBillingApplyDO);

    @Update("update fin_billing_apply set  " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "contract_code = #{contractCode,jdbcType=VARCHAR}," +
            "billing_code = #{billingCode,jdbcType=VARCHAR}," +
            "amount = #{amount,jdbcType=DECIMAL}," +
            "invoice_state = #{invoiceState,jdbcType=VARCHAR}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR}" +
            " where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(FinBillingApplyDO finBillingApplyDO);

    @Update("update fin_billing_apply set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "FinBillingApplyResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "contract_code", property = "contractCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "billing_code", property = "billingCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "amount", property = "amount", jdbcType = JdbcType.DECIMAL),
            @Result(column = "invoice_state", property = "invoiceState", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
    })
    @Select("select " +
            "objuid, fac_code, " +
            "contract_code, billing_code, amount, invoice_state, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply " +
            "where del_flag = 0")
    List<FinBillingApplyDO> selectAll();

    @ResultMap("FinBillingApplyResultMap")
    @Select("select " +
            "objuid, fac_code, " +
            "contract_code, billing_code, amount, invoice_state, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply " +
            "where del_flag = 0")
    List<FinBillingApplyDO> selectPageAll(Page<FinBillingApplyDO> page);

    @ResultMap("FinBillingApplyResultMap")
    @Select("select " +
            "objuid, fac_code, " +
            "contract_code, billing_code, amount, invoice_state, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply " +
            "where del_flag = 0 and objuid = #{id,jdbcType=VARCHAR}")
    FinBillingApplyDO selectOneById(@Param("id") String id);

    @ResultMap("FinBillingApplyResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, " +
            "contract_code, billing_code, amount, invoice_state, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply " +
            "where del_flag = 0" +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.contractCode !=null and o.contractCode != '' \">" +
            "   and contract_code = #{o.contractCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.billingCode !=null and o.billingCode != '' \">" +
            "   and billing_code = #{o.billingCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.invoiceState !=null and o.invoiceState != '' \">" +
            "   and invoice_state = #{o.invoiceState, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<FinBillingApplyDO> selectList(@Param("o") FinBillingApplyDO finBillingApplyDO);

    @ResultMap("FinBillingApplyResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, " +
            "contract_code, billing_code, amount, invoice_state, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply " +
            "where del_flag = 0" +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.contractCode !=null and o.contractCode != '' \">" +
            "   and contract_code = #{o.contractCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.billingCode !=null and o.billingCode != '' \">" +
            "   and billing_code = #{o.billingCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.invoiceState !=null and o.invoiceState != '' \">" +
            "   and invoice_state = #{o.invoiceState, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<FinBillingApplyDO> selectPageList(@Param("o") FinBillingApplyDO finBillingApplyDO, Page<FinBillingApplyDO> page);

    @ResultMap("FinBillingApplyResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, " +
            "contract_code, billing_code, amount, invoice_state, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from fin_billing_apply " +
            "where del_flag = 0" +
            "<if test=\"o.contractCode!=null or o.billingCode!=null\">" +
                "and (" +
                "<trim prefixOverrides=\"or\">" +
                "<if test =\"o.contractCode !=null and o.contractCode != '' \">" +
                "  or contract_code like concat(#{o.contractCode, jdbcType=VARCHAR},'%') " +
                "</if>" +
                "<if test =\"o.billingCode !=null and o.billingCode != '' \">" +
                "   or billing_code like concat(#{o.billingCode, jdbcType=VARCHAR},'%') " +
                "</if>" +
                "</trim>" +
                ")" +
            "</if>" +
            "</script>")
    List<FinBillingApplyDO> selectComboxList(@Param("o") FinBillingApplyDO finBillingApplyDO, Page<FinBillingApplyDO> page);

    @Select("select " +
            "count(objuid) from fin_billing_apply " +
            "where del_flag = 0 and contract_code = #{contractCode, jdbcType=VARCHAR}")
    Integer checkUniqueForContractCode(@Param("contractCode") String contractCode);

    @Select("select " +
            "count(objuid) from fin_billing_apply " +
            "where del_flag = 0 and billing_code = #{billingCode, jdbcType=VARCHAR}")
    Integer checkUniqueForBillingCode(@Param("billingCode") String billingCode);

    @Select("select " +
            "count(objuid) from fin_billing_apply " +
            "where del_flag = 0 and contract_code = #{contractCode, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForContractCodeUpdate(@Param("contractCode") String contractCode, @Param("id") String id);

    @Select("select " +
            "count(objuid) from fin_billing_apply " +
            "where del_flag = 0 and billing_code = #{billingCode, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForBillingCodeUpdate(@Param("billingCode") String billingCode, @Param("id") String id);
}
